package ipad.asia.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import ipad.asia.config.NewSecKillProduct;
import ipad.asia.entity.Product;
import ipad.asia.entity.SeckillProduct;
import ipad.asia.service.product.ProductFeign;
import ipad.asia.service.seckill.SecKillFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class DiscountController {
    @Autowired
    private SecKillFeign secKillFeign;
    @Autowired
    private ProductFeign productFeign;
    @Autowired
    private RedisTemplate redisTemplate;
    @RequestMapping("/discount/secKill")
    public String secKill(@RequestParam(value = "productId",required = false)Integer productId,
                          @RequestParam(value = "price",required = false)String price,
                          @RequestParam(value = "count",required = false) Integer count,
                          @RequestParam(value = "start1",required = false) String start1,
                          @RequestParam(value = "start2",required = false) String start2,
                          @RequestParam(value = "end1",required = false) String end1,
                          @RequestParam(value = "end2",required = false)String end2,
                          Model model,HttpSession session) throws Exception {
        String startDate=start1+" "+start2;String endDate=end1+" "+end2;
        System.out.println(startDate);
        if(price==null){
            List<Product> products = productFeign.getShopId((Integer) session.getAttribute("shopId"));
            model.addAttribute("products",products);
            return "secKillList";
        }
        else {
            System.out.println("product的id"+productId);
            Product product = productFeign.getById(productId);
            int num = product.getNumber() - count;
            if(num<0){
                List<Product> products = productFeign.getShopId((Integer) session.getAttribute("shopId"));
                model.addAttribute("products",products);
                model.addAttribute("message","超出库存！");
                return "secKillList";
            }
            productFeign.updateNumber(num, productId);
            System.out.println("价格：" + price);
            productFeign.updateDiscount(1, productId);
            secKillFeign.addSecKill(productId, Float.parseFloat(price), count, startDate, endDate);
            model.addAttribute("message", "设置成功！");
            List<Product> products = productFeign.getShopId((Integer) session.getAttribute("shopId"));
            model.addAttribute("products", products);
            return "secKillList";
        }
    }


    @RequestMapping("/discount/secKillDel")
    public String secKillDel(@RequestParam(value = "productId",required = false)String productId,
                          Model model,HttpSession session) throws Exception {
        if(productId==null){
            model.addAttribute("message","你已经撤销秒杀，请刷新重试");
            List<Product> products = productFeign.getShopId((Integer) session.getAttribute("shopId"));
            model.addAttribute("products",products);
            return "redirect:/discount/secKill";
        }else {
            Object object = redisTemplate.opsForValue().get("secKill:" + productId+"");
            System.out.println("object:::::::"+object);
            JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(object));
            Product product = productFeign.getById(Integer.parseInt(productId));
            System.out.println("撤销的product"+product);
            int num = (Integer) jsonObject.get("number") + product.getNumber();
            System.out.println("num++++++"+num);
            productFeign.updateNumber(num, Integer.parseInt(productId));
            productFeign.updateDiscount(0, Integer.parseInt(productId));
            secKillFeign.deleteSecKill(Integer.valueOf(productId));
            return "redirect:/discount/secKill";
        }
    }


    @RequestMapping("/discount/secKillList")
    public String secKillList(@RequestParam(value = "productId",required = false)String productId,
                             Model model,HttpSession session) throws Exception {
        List<Product> products = productFeign.getShopId((Integer) session.getAttribute("shopId"));
        List<SeckillProduct> secKillProducts=secKillFeign.products();
        List<NewSecKillProduct> newSecKillProducts=new ArrayList<>();
        //两个产品按照id的属性合并
        for(int i=0;i<products.size();i++){
           for(int j=0;j<secKillProducts.size();j++){
               if(products.get(i).getId()==secKillProducts.get(j).getProductId()){
                   NewSecKillProduct newSecKillProduct=new NewSecKillProduct();
                   SeckillProduct seckillProduct=secKillFeign.getById(secKillProducts.get(j).getProductId());
                   Product product=productFeign.getById(products.get(i).getId());
                   newSecKillProduct.setProduct(product);
                   newSecKillProduct.setSeckillProduct(seckillProduct);
                   newSecKillProducts.add(newSecKillProduct);
               }
           }
        }
        model.addAttribute("data",newSecKillProducts);
        return "haveSecKillList";
    }

}
